package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Evaluation;
import cn.openea.perpartner.meily.web.entity.dto.SearchDto;
import cn.openea.perpartner.meily.web.service.IEvaluationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Evaluation)表控制层
 *
 * @author Weird077
 * @since 2023-01-29 15:55:45
 */
@Api(tags = "测评需求")
@RestController
@RequestMapping("/evaluation")
public class EvaluationController extends BaseController {
    static final String PREFIX = "CPXQ";
    @Resource
    private IEvaluationService evaluationService;

    @ApiOperation("需求列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SearchDto dto) {
        startPage();
        List<Evaluation> list = this.evaluationService.selectList(dto);
        return getDataTable(list);
    }

    @ApiOperation("需求详情")
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.evaluationService.getById(id));
    }

    @ApiOperation("新增需求")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Evaluation evaluation) {
        if (StrUtil.isBlank(evaluation.getDemandNo())){
            evaluation.setDemandNo(evaluationService.createDemandNo(PREFIX,3));
        }
        evaluation.setCreateUser(UserContext.getUserId());
        evaluation.setCreateUserName(UserContext.getUserName());
        evaluation.setUpdateUser(UserContext.getUserId());
        evaluation.setUpdateTime(DateUtil.now());
        evaluation.setCreateTime(DateUtil.now());
        return toAjax(this.evaluationService.save(evaluation));
    }

    @ApiOperation("修改需求")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Evaluation evaluation) {
        evaluation.setUpdateUser(UserContext.getUserId());
        evaluation.setUpdateTime(DateUtil.now());
        return toAjax(this.evaluationService.updateById(evaluation));
    }
    @ApiOperation("删除需求")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<String> idList) {
        return toAjax(this.evaluationService.removeByIds(idList));
    }

    @ApiOperation("生成编号")
    @GetMapping("/create/code")
    public AjaxResult createDemandNo(){
        return AjaxResult.success("操作成功",this.evaluationService.createDemandNo(PREFIX,3));
    }

    @ApiOperation("数据迁移")
    @PostMapping("/migrate")
    public AjaxResult migrate(@RequestBody List<Evaluation> evaluationList) {
        return toAjax(this.evaluationService.migrate(evaluationList));
    }

}

